﻿using AnesSystem.Models;
using AnesSystem.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AnesSystem.Controls
{
    /// <summary>
    /// AccountListCtrl.xaml 的交互逻辑
    /// </summary>
    public partial class AccountListCtrl : UserControl
    {
        
        public AccountListCtrl()
        {
            InitializeComponent();
            this.PreviewKeyUp += AccountListCtrl_PreviewKeyUp;
            _listView.GotFocus += _listView_GotFocus;
            this.DataContext = AccountListCtrlViewModel.Instance;
        }
        
        private void AccountListCtrl_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key== Key.Enter)
            {
                if (_listView.SelectedValue != null)
                {
                    SelectedValue = _listView.SelectedValue as AccountModel;
                    ItemClicked?.Invoke(sender, SelectedValue);
                }
            }
        }

        public int SelectedIndex
        {
            get
            {
                return _listView.SelectedIndex;
            }
            set
            {
                _listView.SelectedIndex = value;
            }
        }

        public bool IsFocus { get; set; }

        public AccountModel SelectedValue
        {
            get { return (AccountModel)GetValue(SelectedVaueProperty); }
            set { SetValue(SelectedVaueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedVaue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedVaueProperty =
            DependencyProperty.Register("SelectedValue", typeof(AccountModel), typeof(AccountListCtrl), new PropertyMetadata(null));


        public void RefViewBySerchWord(string word)
        {
            AccountListCtrlViewModel.Instance.Serch(word);
        }

        public event EventHandler<AccountModel> ItemClicked;

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SelectedValue = (sender as TextBlock).DataContext as AccountModel;
            ItemClicked?.Invoke(sender, SelectedValue);
        }

        private void _listView_GotFocus(object sender, RoutedEventArgs e)
        {
            _listView.SelectedIndex = 0;
        }

    }
}
